package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.CityDao;
import com.baichang.taoshunda.dao.IdleCollectionDao;
import com.baichang.taoshunda.dao.IdleGoodsDao;
import com.baichang.taoshunda.entity.City;
import com.baichang.taoshunda.entity.IdleCollection;
import com.baichang.taoshunda.entity.IdleGoods;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.IdleGoodsService;
import com.baichang.taoshunda.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/29 0029.
 */
@Service
public class IdleGoodsServiceImpl implements IdleGoodsService {

    @Autowired
    private IdleGoodsDao idleGoodsDao;

    @Autowired
    private CityDao cityDao;

    @Autowired
    private IdleCollectionDao idleCollectionDao;

    @Autowired
    private UserService userService;

    /**
     * @param name
     * @param sunmmary
     * @param picture
     * @param newPrice
     * @param oldPrice
     * @param cityId
     * @param areaId   @return
     * @param userId
     * @param phone
     * @param id
     */
    @Override
    public boolean addIdleGoods(String name, String sunmmary, String picture, BigDecimal newPrice, BigDecimal oldPrice, Integer cityId, Integer areaId, Integer userId, String phone, Integer id) throws ServiceException {
        if (cityId == 0) {
            throw new ServiceException("请选择城市");
        }
        if (picture.equals("")) {
            throw new ServiceException("请上传图片");
        }
        IdleGoods idleGoods = new IdleGoods();
        idleGoods.setStatus(1);
        idleGoods.setName(name);
        idleGoods.setCreatedTime(new Date());
        idleGoods.setIdlePic(picture);
        idleGoods.setNewPrice(newPrice);
        idleGoods.setOldPrice(oldPrice);
        idleGoods.setSunmmary(sunmmary);
        if (cityId > 0) {
            //城市名字
            City city = cityDao.getCityById(cityId);
            //查询省级名字
            Map<String, Object> map = cityDao.getCityInfo(city.getParentId());
            String provincesName = map.get("name").toString();
            //转为long
            Long a = areaId.longValue();
            Map<String, Object> maps = cityDao.getCityInfo(a);
            String areaName = maps.get("name").toString();
            idleGoods.setAddress(city.getName() + "" + areaName);
        }
        idleGoods.setCityId(cityId);
        idleGoods.setAreaId(areaId);
        idleGoods.setUserId(userId);
        idleGoods.setPhone(phone);
        if (id != 0) {
            idleGoods.setId(id);
            return idleGoodsDao.update(idleGoods) > 0;
        } else {
            return idleGoodsDao.save(idleGoods) > 0;
        }

    }

    /**
     * 查询闲置物品
     *
     * @param userId
     * @param name
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> queryIdleGoods(Integer userId, Integer cityId, Integer areaId, String name, int nowPage, int pageSize) {
        return idleGoodsDao.queryIdleGoods(userId, cityId, areaId, name, nowPage, pageSize);
    }

    /**
     * 查看闲置详细
     *
     * @param idleId
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> queryIdleGoodsDetails(Integer idleId, Integer userId) throws ServiceException {

        Map<String, Object> mapIdle = idleGoodsDao.queryIdleGoodsDetails(idleId);
        if (mapIdle != null && !mapIdle.isEmpty()) {
            //查看是否收藏过
            Map<String, Object> maps = idleCollectionDao.getControllerInfo(idleId, userId);
            String collection = maps.get("collection").toString();
            User user = userService.getUserBeanById(Integer.parseInt(mapIdle.get("userId").toString()));
            //1收藏 2未收藏
            mapIdle.put("collection", collection);
            mapIdle.put("userName", user.getNickName());
            mapIdle.put("ryUserId", JSONObject.fromObject(user.getRyToken()).get("userId"));
            mapIdle.put("ryToken", JSONObject.fromObject(user.getRyToken()).get("token"));
        } else {
            mapIdle = new HashMap<>();
            mapIdle.put("status", "0");
        }
        return mapIdle;

    }

    /**
     * 收藏闲置商品
     *
     * @param idleId
     * @param userId
     * @param
     * @return
     */
    @Override
    public boolean addIdleCollection(Integer idleId, Integer userId) {//查看是否收藏过
        Map<String, Object> maps = idleCollectionDao.getControllerInfo(idleId, userId);
        String collection = maps.get("collection").toString();
        int ifCollection = Integer.parseInt(collection);
        if (ifCollection == 0) {
            IdleCollection idleCollection = new IdleCollection();
            idleCollection.setUserId(userId);
            idleCollection.setIdleGoodsId(idleId);
            return idleCollectionDao.save(idleCollection) > 0;
        } else {
            return idleCollectionDao.deleteCollection(idleId, userId) > 0;
        }
    }

    /**
     * 删除闲置商品
     *
     * @param idleId
     * @return
     */
    @Override
    public boolean deleteIdle(Integer idleId) {
        return idleGoodsDao.deleteIdle(idleId) > 0;
    }

    /**
     * 闲置商品分享
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getIdleGoodsById(Integer id) {
        Map<String, Object> map = idleGoodsDao.getIdleGoodsById(id);
        if (map != null) {
            String pic = map.get("idlePic").toString();
            String[] a = pic.split(",");
            String b = a[0].toString();
            map.put("onePic", b);
        }
        return map;
    }

    /**
     * 查询收藏列表
     *
     * @param userId   用户编号
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> findIdleCollection(Integer userId, Integer nowPage, int pageSize) {
        return idleGoodsDao.findIdleCollection(userId, nowPage, pageSize);
    }
}
